package cn.teacher.service.impl;

import cn.teacher.config.web.DataGridResultInfo;
import cn.teacher.dao.CompetitionPlanDaoMapper;
import cn.teacher.pojo.CompetitionPlan;
import cn.teacher.pojo.dto.CompetitionInfoDTO;
import cn.teacher.pojo.dto.CompetitionPlanDTO;
import cn.teacher.pojo.dto.RegisterCompetitionDTO;
import cn.teacher.service.CompetitionPlanService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/25
 * Time: 19:23
 * Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CompetitionPlanServiceImpl implements CompetitionPlanService {

    @Autowired
    private CompetitionPlanDaoMapper competitionPlanDaoMapper;

    @Override
    public boolean add(CompetitionPlan competitionPlan) {
        competitionPlan.setCreateTime(System.currentTimeMillis());
        competitionPlan.setUpdateTime(System.currentTimeMillis());
        return competitionPlanDaoMapper.insert(competitionPlan) > 0 ? true : false;
    }

    @Override
    public boolean remove(long cpId) {
        return competitionPlanDaoMapper.delete(cpId) > 0 ? true : false;
    }

    @Override
    public boolean modify(CompetitionPlan competitionPlan) {
        competitionPlan.setUpdateTime(System.currentTimeMillis());
        return competitionPlanDaoMapper.update(competitionPlan) > 0 ? true : false;
    }

    @Override
    public DataGridResultInfo<CompetitionPlanDTO> find(long userId, int page, int rows) {
        Page<CompetitionPlanDTO> competitionPlanDTOS = PageHelper.startPage(page, rows);
        competitionPlanDaoMapper.selectCompetitionPlanByUserId(userId);
        DataGridResultInfo<CompetitionPlanDTO> dataGridResultInfo = new DataGridResultInfo<>();
        dataGridResultInfo.setEndRow(competitionPlanDTOS.getEndRow());
        dataGridResultInfo.setPageNum(competitionPlanDTOS.getPageNum());
        dataGridResultInfo.setPages(competitionPlanDTOS.getPages());
        dataGridResultInfo.setPageSize(competitionPlanDTOS.getPageSize());
        dataGridResultInfo.setRows(competitionPlanDTOS.getResult());
        dataGridResultInfo.setStartRow(competitionPlanDTOS.getStartRow());
        dataGridResultInfo.setTotal(competitionPlanDTOS.getTotal());
        return dataGridResultInfo;
    }

    @Override
    public DataGridResultInfo<RegisterCompetitionDTO> findRegisterCompetitionDTO(long userId, int page, int rows) {
        Page<RegisterCompetitionDTO> competitionPlanDTOS = PageHelper.startPage(page, rows);
        competitionPlanDaoMapper.selectRegisterCompetitionDTO(userId);
        DataGridResultInfo<RegisterCompetitionDTO> dataGridResultInfo = new DataGridResultInfo<>();
        dataGridResultInfo.setEndRow(competitionPlanDTOS.getEndRow());
        dataGridResultInfo.setPageNum(competitionPlanDTOS.getPageNum());
        dataGridResultInfo.setPages(competitionPlanDTOS.getPages());
        dataGridResultInfo.setPageSize(competitionPlanDTOS.getPageSize());
        dataGridResultInfo.setRows(competitionPlanDTOS.getResult());
        dataGridResultInfo.setStartRow(competitionPlanDTOS.getStartRow());
        dataGridResultInfo.setTotal(competitionPlanDTOS.getTotal());
        return dataGridResultInfo;
    }

    @Override
    public DataGridResultInfo<CompetitionInfoDTO> findCompetitionPlans(int page, int rows) {
        Page<CompetitionInfoDTO> competitionInfoDTOS = PageHelper.startPage(page, rows);
        competitionPlanDaoMapper.selectCompetitionPlans();
        DataGridResultInfo<CompetitionInfoDTO> dataGridResultInfo = new DataGridResultInfo<>();
        dataGridResultInfo.setEndRow(competitionInfoDTOS.getEndRow());
        dataGridResultInfo.setPageNum(competitionInfoDTOS.getPageNum());
        dataGridResultInfo.setPages(competitionInfoDTOS.getPages());
        dataGridResultInfo.setPageSize(competitionInfoDTOS.getPageSize());
        dataGridResultInfo.setRows(competitionInfoDTOS.getResult());
        dataGridResultInfo.setStartRow(competitionInfoDTOS.getStartRow());
        dataGridResultInfo.setTotal(competitionInfoDTOS.getTotal());
        return dataGridResultInfo;
    }

    @Override
    public CompetitionInfoDTO findCompetitionPlan(long cpId) {
        return competitionPlanDaoMapper.selectOne(cpId);
    }

}
